﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Database;

namespace ConnectDatabase
{
    public class AirlinesDB
    {
        DatabaseDataContext dbContext = new DatabaseDataContext();

        #region Các hàm cơ bản
        public List<tblAirline> GetAll()
        {
            try
            {
                var list = from a in dbContext.tblAirlines where a.Visible select a;
                return list.ToList<tblAirline>();
            }
            catch
            {
                return new List<tblAirline>();
            }
        }

        public tblAirline GetByKey(string code)
        {
            try
            {
                var obj = dbContext.tblAirlines.SingleOrDefault(a => a.Code == code && a.Visible);
                if (obj == null)
                {
                    obj = new tblAirline();
                    obj.Code = code;
                    obj.Name_Vi = code;
                    obj.Name_En = code;
                    obj.Name_Fr = code;
                    obj.BSP = false;
                    obj.Description = "";
                    obj.Visible = true;
                    obj.Logo = "";
                }
                return obj;
            }
            catch
            {
                return null;
            }
        }

        public bool Insert(tblAirline obj)
        {
            bool isOk = false;
            try
            {
                dbContext.tblAirlines.InsertOnSubmit(obj);
                dbContext.SubmitChanges();
                isOk = true;
            }
            catch
            {
                return false;
            }
            return isOk;
        }

        public bool Update(tblAirline obj)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblAirlines.SingleOrDefault(a => a.Code == obj.Code);
                if (exist != null)
                {
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }

        public bool Delete(string code)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblAirlines.SingleOrDefault(a => a.Code == code);
                if (exist != null)
                {
                    exist.Visible = false;
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }
        #endregion
    }
}
